-- SPDX-License-Identifier: GPL-3.0-or-later
---@class StoryData: Object
local OtherEvent = class("OtherEvent")
local WU = require "packages/wdtx/WUltiity"
local Story = require "packages/wdtx/Story"
-- local Map = require "packages/wdtx/Map"
function OtherEvent:initialize(player,data,obj)
  self.player = player
  self.wdtxobj = obj
  self.eventdata = data
  self.istri_event = {
    wuanguo = false
  }
  self.ext_event_rwd = {
    wuanguo = {
      talk = "谢了，小兄弟这点东西请收下。",
      getskills = {"diezhang","duanwan"},
      openBox = true
    }
  }
  self:initEventData()
end

function OtherEvent:initEventData()
  local eventdata = self:getEventData()
  self.normal = table.clone(eventdata.normal)
  self.advanced = table.clone(eventdata.advanced)
end

function OtherEvent:curPlayerisTriEvent(general)
  return self.istri_event[general]
end

function OtherEvent:isTriEvent()
  local result
  for evt,ret in pairs(self.istri_event) do
    if ret then
      result = evt
    end
  end
  return result
end

function OtherEvent:extEventRwd(evt)
  local room = self.player.room
  local ret = self.ext_event_rwd[evt]
  if ret then
    local curevttarget = table.find(room.players,function(pr) return pr.general == evt end)
    if ret.talk then
      curevttarget:chat(ret.talk)
    end
    if ret.getskills then
      local getskills = table.concat(ret.getskills,"|")
      room:handleAddLoseSkills(self.player,getskills,nil,false)
      room:doBroadcastNotify("ShowToast","<font color='yellow'><b>你获得了"..Fk:translate(evt).."的技能。</b></font>")
    end
    if ret.openBox then
      self.wdtxobj:openBox(2)
    end
    self.istri_event[evt] = false
    self.wdtxobj:changeSoldier(curevttarget)
    if not curevttarget.dead then
      room:killPlayer({
        who = curevttarget.id
      })
    end
  end
end

function OtherEvent:getEventData()
  return self.eventdata[self.wdtxobj.storyid]
end

function OtherEvent:getAdvancedEventData()
  return self.advanced
end

function OtherEvent:exeCharacterEvt(who,reply)
  local player = self.player
  local room = player.room
  if who == "huatuo" then
    if reply.selchc == "流失1点体力" then
      room:loseHp(player,1,"#wdtx_rule")
      room:setPlayerProperty(player, "maxHp", player.maxHp+1)
    elseif reply.selchc == "获得1张桃" then
      self.wdtxobj:addInitCardsToArea({wdtx__peach = 1})
    end
  elseif who == "ty__xushao" then
    if reply.selchc == "交给最多3张卡牌。<font color='red'><b>（移除）</b></font>" then
      local total = 0
      local ret,selnum = self.wdtxobj:removeCardsToVoid(3,true,who,"君之宝物甚多。（交给许邵最多3张牌）",true)
      if not ret then return room:doBroadcastNotify("ShowToast", Fk:translate(who) .."<font color='red'><b>不屑的走了..</b></font>") end
      total = total + selnum
      if next(ret) then
        local num = 0
        for cid,upnum in pairs(ret) do
          num = num + 1
        end
        total = total + num
      end
      if total >= 4 then
        local str = "<font color='green'><b>获得【荐才】卡牌。</b></font>"
        self.wdtxobj:addInitCardsToArea({wdtx__jiancai = 1})
        if total >= 6 then
          str = str.."<font color='green'><b>再获得【荐才】卡牌。</b></font>"
          self.wdtxobj:addInitCardsToArea({wdtx__jiancai = 1})
          -- room:handleAddLoseSkills(player,"ty__pingjian",nil,false)
        end
        room:doBroadcastNotify("ShowToast", Fk:translate(who) .."：<font color='green'><b>足下乃当世英雄。请收下此物。</b></font>"..Fk:translate(player.general)..str)
      else
        room:doBroadcastNotify("ShowToast", Fk:translate(who) .."<font color='red'><b>不屑的走了..</b></font>")
      end
    elseif reply.selchc == "交给1个普通技能" then
      local chc = WU:getPlayerSkills(player)
      local chs = room:askForChoice(player, chc, "#wdtx_rule", "#wdtx_rule")
      room:handleAddLoseSkills(player,"-"..chs,nil,false)
      room:handleAddLoseSkills(player,"wdtx_xianyu",nil,false)
      room:doBroadcastNotify("ShowToast", Fk:translate(who) .."看你不过是碌碌无为之人.."..Fk:translate(player.general).."获得【咸鱼】技能。")
    elseif reply.selchc == "交给20金币" then
      self.wdtxobj:costGold(20)
      self.wdtxobj:upCardByPile(2,who,"君真是多财多艺。请升级最多2张牌。不枉此行。")
    end
  elseif who == "hxfc_author" then
    if reply.selchc == "回满体力" then
      room:recover({
        who = player,
        num = player:getLostHp(),
        skillName = "wdtx_rule"
      })
    elseif reply.selchc == "升级1张卡牌" then
      self.wdtxobj:upCardByPile(1)
    else
      self.wdtxobj:getGold(15)
    end
  elseif who == "hj_daoqibing" then
    local fight_func = function(talk)  
      self.wdtxobj:deployEnemys(
        {
          talkinfo = {who="hj_daoqibing",talk=talk},
          gens = {{gen="hj_daoqibing",strength=2,normonskills=1},{gen="hj_daobing",strength=1},{gen="hj_gongshou",strength=1}},
        }
      )--"hj_daobing","hj_gongshou"
      self.wdtxobj:setStoryStatus(Story.StatusFight)
      -- self.wdtxobj:setCurInfo({startpos = 0,selchcinx = 0}) -- 其他事件的战斗胜利结束后需要清掉此剧情相关索引才可以进入下一个剧情
    end
    if reply.selchc == "就凭你们？【准备战斗】" then
      fight_func("我看你是不想活了。兄弟们上！！")
    elseif reply.selchc == "交给20金币" then
      self.wdtxobj:costGold(20)
      room:doBroadcastNotify("ShowToast", Fk:translate(who) .."：真是识相。你可以安全的离开了。")
    elseif reply.selchc == "看！有飞碟（溜了溜了~）" then
      local rand = math.random()      
      if rand < 0.5 then
        fight_func("想骗我？兄弟们给我好好收拾他！")
      else
        room:doBroadcastNotify("ShowToast","你侥幸逃过了一劫。")
      end
    end
  elseif who == "js__liubei" then
    if reply.selchc == "赠与5金币" then
      room:doBroadcastNotify("ShowToast", Fk:translate(who) .."皱了皱眉头表示：勿以恶小而为之，勿以善小而不为。说完走了。")
    elseif reply.selchc == "赠与15金币" then
      self.wdtxobj:costGold(15)
      self.wdtxobj:addInitCardsToArea({wdtx__zhenqiao = 1})
      room:doBroadcastNotify("ShowToast", Fk:translate(who) .."给了你一张【振鞘】卡牌。")
    elseif reply.selchc == "赠与25金币" then
      self.wdtxobj:costGold(25)
      room:handleAddLoseSkills(player,"jishan",nil,false)
      room:doBroadcastNotify("ShowToast", Fk:translate(who) .."给了你【积善】技能。")
    end
  elseif who == "cuiyan" then
    if reply.selchc == "读万卷书行万里路" then
      self.wdtxobj:addInitCardsToArea({wdtx__boshi = 1})
      room:doBroadcastNotify("ShowToast", Fk:translate(who) .."送给你一张【博识】卡牌")
    elseif reply.selchc == "书中自有黄金屋" then
      self.wdtxobj:getGold(20)
      room:doBroadcastNotify("ShowToast", Fk:translate(who) .."给了你一些钱")
    elseif reply.selchc == "我已经不是小孩子了。全都要！" then
      room:doBroadcastNotify("ShowToast", Fk:translate(who) .."：什么都要。不如看运气。")
      local rand = math.random(10,30)
      self.wdtxobj:getGold(rand)
    end
  elseif who == "shanzei_boss" then
    if reply.selchc == "可恶，只好拼了" then
      self.wdtxobj:deployEnemys(
        {
          gens = {{gen="shanzei_boss",strength=1,normonskills=1},{gen="shanzei_loulou"},{gen="shanzei_loulou"}},
        }
      )
      self.wdtxobj:setStoryStatus(Story.StatusFight)
    end
  elseif who == "hulao2__godlvbu" then
    if reply.selchc == "帮助武安国" then
      self.istri_event.wuanguo = true
      self.wdtxobj:deployEnemys(
        {
          talkinfo = {who=who,talk="螳臂当车。这么想死成全你。"},
          gens = {{gen="hulao2__godlvbu",strength=4,normonskills=1,extraskills={"ol_ex__wansha","shenqu","ty_ex__xuanfeng","qiangxi"}},{gen="wuanguo",role="loyalist",strength=2,normonskills=1,extraskills={"ex__yingzi"}}},
        }
      )
      self.wdtxobj:setStoryStatus(Story.StatusFight)
    end
  end
end

function OtherEvent:creatSubDialogByChaEvt(who,reply)
  local chcevent = reply.chcevent
  if who == "huatuo" then
    local opchc = {}
    if self.player.hp > 1 then
      table.insert(opchc,"流失1点体力")
    end
    table.insert(opchc,"获得1张桃")
    table.insert(opchc,"父精母血，不可流失！转头离开")
    self.wdtxobj:addSubDialog({
      dialog = {{talkers={1,who,2,self.player.general},say="如果你健康身体<font color='red'><b>流失1点体力</b></font>，我就能做成一副药。服下让你更健壮。<font color='green'>（体力上限+1）</b></font>"}},
      choices = {"流失1点体力","获得1张桃","父精母血，不可流失！转头离开"},
      op_choices = opchc,
      eventchc = {chcevent+1,chcevent+1,chcevent+1}
    },1,2)
  elseif who == "ty__xushao" then
    local opchc = {}
    table.insert(opchc,"交给最多3张卡牌。<font color='red'><b>（移除）</b></font>")
    if #WU:getPlayerSkills(self.player) > 0 then
      table.insert(opchc,"交给1个普通技能")
    end
    if self.player:getMark("@gold_num") >= 20 then
      table.insert(opchc,"交给20金币")
    end
    table.insert(opchc,"离开")
    self.wdtxobj:addSubDialog({
      dialog = {{talkers={1,who,2,self.player.general},say="君不显才，我不能评价。"}},
      choices = {
        "交给最多3张卡牌。<font color='red'><b>（移除）</b></font>",
        "交给1个普通技能",
        "交给20金币",
        "离开",
      },
      op_choices = opchc,
      eventchc = {chcevent+1,chcevent+1,chcevent+1,chcevent+1}
    },1,2)
  elseif who == "shanzei_boss" then
    local opchc = {"可恶，只好拼了"}
    self.wdtxobj:addSubDialog({
      dialog = {{talkers={1,who,2,self.player.general},say="又来了个小肥羊。兄弟们开席！"}},
      choices = opchc,
      op_choices = opchc,
      eventchc = {chcevent+1}
    },1,2)
  elseif who == "hj_daoqibing" then
    local opchc = {}
    table.insert(opchc,"就凭你们？【准备战斗】")
    if self.player:getMark("@gold_num") >= 20 then
      table.insert(opchc,"交给20金币")
    end
    table.insert(opchc,"看！有飞碟（溜了溜了~）")
    self.wdtxobj:addSubDialog({
      dialog = {{talkers={1,who,2,self.player.general},say="站住，留下买路财！!"}},
      choices = {
        "就凭你们？【准备战斗】",
        "交给20金币",
        "看！有飞碟（溜了溜了~）",
      },
      op_choices = opchc,
      eventchc = {chcevent+1,chcevent+1,chcevent+1}
    },1,2)
  elseif who == "js__liubei" then
    local opchc = {}
    local playergold = self.player:getMark("@gold_num")
    if playergold >= 5 then
      table.insert(opchc,"赠与5金币")
    end
    if playergold >= 15 then
      table.insert(opchc,"赠与15金币")
    end
    if playergold >= 25 then
      table.insert(opchc,"赠与25金币")
    end
    table.insert(opchc,"离开")
    self.wdtxobj:addSubDialog({
      dialog = {{talkers={1,who,2,self.player.general},say="我乃中山靖王之后。无奈家道中落。沦落至此。壮士若愿资备一些钱，必有大谢。"}},
      choices = {
        "赠与5金币",
        "赠与15金币",
        "赠与25金币",
        "离开"
      },
      op_choices = opchc,
      eventchc = {chcevent+1,chcevent+1,chcevent+1,chcevent+1}
    },1,2)
  elseif who == "cuiyan" then
    local opchc = {
      "读万卷书行万里路",
      "书中自有黄金屋",
      "我已经不是小孩子了。全都要！",
    }
    self.wdtxobj:addSubDialog({
      dialog = {{talkers={1,who,2,self.player.general},say="你喜欢【读万卷书行万里路】。还是【书中自有黄金屋】？"}},
      choices = opchc,
      op_choices = opchc,
      eventchc = {chcevent+1,chcevent+1,chcevent+1}
    },1,2)
  elseif who == "hulao2__godlvbu" then
    local opchc = {
      "帮助武安国",
      "无关我事，离开",
    }
    self.wdtxobj:addSubDialog({
      dialog = {{talkers={1,"wuanguo",2,who},say="吕布！断我臂膀，跟你拼了！"}},
      choices = opchc,
      op_choices = opchc,
      eventchc = {chcevent+1,chcevent+1}
    },1,2)
  elseif who == "hxfc_author" then
    local opchc = {
      "回满体力",
      "升级1张卡牌",
      "金币+15",
    }
    self.wdtxobj:addSubDialog({
      dialog = {{talkers={1,who,2,self.player.general},say="我在琢磨新的内容。一点小东西凑合用。"}},
      choices = opchc,
      op_choices = opchc,
      eventchc = {chcevent+1,chcevent+1,chcevent+1}
    },1,2)
  end
end

function OtherEvent:getCurOtherEvt(ty)
  -- if #self[ty] == 0 then return end
  local curprogress = self.wdtxobj:getCurProgress()
  local randevt
  local count = 0
  while not randevt do
    if count >= 50 then
      randevt = {type = "character",area={0,1},who ="hxfc_author"}
    end
    if #self[ty] > 0 then
      local randpos = math.random(1,#self[ty])
      local area = self[ty][randpos].area
      if curprogress >= area[1] and curprogress <= area[2] then
        randevt = table.remove(self[ty],randpos)
      end
    end
    count = count + 1
  end
  return randevt
end

function OtherEvent:exeOtherEvent(reply)
  if reply.chcevent == 1 then
    local randevt = self:getCurOtherEvt("normal")
    -- local randevt = {type = "character",area={0,1},who ="hulao2__godlvbu"}
    self.curevtinfo = randevt
    if randevt.type == "character" then
      self:creatSubDialogByChaEvt(randevt.who,reply)
    else

    end
    self.wdtxobj:setStoryStatus(Story.StatusChoice)
  else
    if self.curevtinfo.type == "character" then
      self:exeCharacterEvt(self.curevtinfo.who,reply)
    else

    end
    local status = self.wdtxobj:getStoryStatus()
    if status ~= Story.StatusFight then
      self.wdtxobj:setStoryStatus(Story.StatusChoice)
    end
  end
end

return OtherEvent
